package mybatis;

import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import java.io.IOException;
import java.util.Properties;

/**
 * @author honway.liu
 * @date 2017/8/13 下午11:21
 * @email gm100861@gmail.com
 * @blog http://linuxsogood.org
 */
public class JavaCodeDemo {

    public static void main(String[] args) throws IOException {
        Properties properties = Resources.getResourceAsProperties("jdbc.properties");
        UnpooledDataSource dataSource = new UnpooledDataSource();
        dataSource.setDriver(properties.getProperty("driver"));
        dataSource.setUrl(properties.getProperty("url"));
        dataSource.setPassword(properties.getProperty("password"));
        dataSource.setUsername(properties.getProperty("username"));

        JdbcTransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("dev", transactionFactory, dataSource);
        Configuration configuration = new Configuration(environment);
        configuration.setCacheEnabled(true);    // 开启二级缓存
        configuration.setMapUnderscoreToCamelCase(true);    // 开启驼峰匹配

        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(configuration);
        SqlSession sqlSession = sessionFactory.openSession();

    }
}
